<template>
  <div>
 
      <!-- 名称：<el-input v-model="empquery.emp_name" placeholder=""></el-input>
      部门：<el-select v-model="empquery.dept_id" placeholder="请选择">
        <el-option label="全部" :value="0"></el-option>
        <el-option label="部门1" :value="1"></el-option>
        <el-option label="部门2" :value="2"></el-option>
        <el-option label="部门3" :value="3"></el-option>
        <el-option label="部门4" :value="4"></el-option>
      </el-select>
      <el-button icon="el-icon-circle-plus-outline" @click="qyery()">查询</el-button> -->



    <el-container>
        <el-button icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true">添加</el-button>
        <el-button icon="el-icon-delete" @click="deletes()">删除</el-button>
    </el-container>
    <el-table :data="tableData"  @selection-change="handleSelectionChange">
    <el-table-column
    prop="emp_id"
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column prop="emp_id" align="center" label="编号" width="140">
      </el-table-column>
      <el-table-column prop="emp_name" align="center" label="姓名" width="120">
      </el-table-column>
      <el-table-column
        prop="emp_sex"
        align="center"
        clign="center"
        label="性别"
      >
      </el-table-column>
      
      <el-table-column
        prop="dept_name"
        align="center"
        clig="center"
        label="部门"
      >
      </el-table-column>

      <el-table-column
        prop="emp_duty"
        align="center"
        clig="center"
        label="信息"
      >
      </el-table-column>

      <el-table-column prop="emp_id" align="center" label="操作" width="300">
        <template v-slot="bb">
          <div>
            <el-button @click="updateEmp1(bb.row)" type="warning">编辑</el-button>
            <el-button @click="deleteEmp(bb)" type="danger">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>


<!-- Form -->

<el-dialog title="添加" :visible.sync="dialogFormVisible">
  <el-form>
    <el-form-item label="编号">
      <el-input v-model="emp.emp_id"></el-input>
    </el-form-item>
    <el-form-item label="名称">
      <el-input v-model="emp.emp_name"></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <el-input v-model="emp.emp_sex"></el-input>
    </el-form-item>
    <el-form-item label="部门">
      <!-- <el-input v-model="emp.dept_name"></el-input> -->
      <el-select v-model="emp.dept_id" placeholder="请选择">
        <el-option label="部门1" :value="1"></el-option>
        <el-option label="部门2" :value="2"></el-option>
        <el-option label="部门3" :value="3"></el-option>
        <el-option label="部门4" :value="4"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="信息">
      <el-input v-model="emp.emp_duty"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="saveEmp()" >添加</el-button>
    <el-button type="primary" @click="updateEmp()">保存</el-button>
  </div>
</el-dialog>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 4, 5, 6]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      empquery: {
        dept_id: 0,
        emp_name: "",
 
      },
        ids:[],
      emp: {
        emp_id:0,
        dept_name: "", 
        dept_id: 1,
        emp_name: "",
        emp_sex: "",
        emp_duty: "",
      },
      total: 4,
      tableData: [],
      currentPage: 1,
      pageSize: 4,

      dialogFormVisible : false,
      // currentPage4: 1
    };
  },
  created() {
    this.qyery();
  },
  methods: {
    saveEmp(){
        this.axios.post("/api/saveEmp.action",this.emp).then(() => {
        alert("添加成功");
        this.dialogFormVisible = false
        this.qyery();
      });

    },
    deletes(){
         let idss=[]

        for(let i=0;i<this.ids.length;i++){
            idss.push(this.ids[i].emp_id)
           
        }

    
        for(let u=0;u<idss.length;u++){
            for(let j=0;j<this.tableData.length;j++){
                if(this.tableData[j].emp_id==idss[u])
                this.tableData.splice(j, 1);
            }
        }
        
        this.axios.post("/api/deletesEmp.action?ids=" + idss).then(data => {
        alert("批量删除成功");
        this.qyery();
      });
    },
    handleSelectionChange(val){
        // console.log(val);
        this.ids=val
    },
    updateEmp1(data) {
        this.emp=data
     
        this.dialogFormVisible=true
    },
    updateEmp(data) {
      this.axios.post("/api/updateEmp.action",this.emp).then(() => {
        alert("编辑成功");
        this.dialogFormVisible = false
        this.qyery();
      });
    },
    deleteEmp(data) {
      console.log(data);
      console.log(data.$index);
      this.tableData.splice(data.$index, 1);
      this.axios.post("/api/deleteEmp.action?id="+data.row.emp_id).then(data=>{
          alert("删除成功")
          this.qyery()
      })
    },
    qyery() {
      
      this.axios
        .post("/api/queryEmpPage.action", {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          total: this.total,
          conditions:this.empquery
        })
        .then((data) => {
          //   alert("dd")
          console.log(data.data.rows);
          this.tableData = data.data.rows;
          this.total = data.data.total;
        });
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.qyery();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.qyery();
      console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style>
</style>